package com.tplentiful.log.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.Data;

import javax.json.Json;

/**
 * @Author: tplentiful
 * @Since: 1.0
 **/
@Data
public class TR<T> {
    public static final Integer OK = 0;
    public static final Integer FAIL = 500;
    private int code;
    private String msg;
    private T data;

    private TR(Integer code, String msg) {
        this(code, msg, null);
    }

    private TR(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static TR<Void> ok(String msg) {
        return new TR<>(OK, msg);
    }

    public static <T> TR<T> ok(String msg, T data) {
        return new TR<T>(OK, msg, data);
    }

    public static TR<Void> fail(String msg) {
        return new TR<>(FAIL, msg);
    }


    public T getData(T tClazz) {
        T data = getData();
        return JSON.parseObject(JSON.toJSONString(data), new TypeReference<T>() {
        });
    }

    public T getData(Class<?> clazz) {
        T data = getData();
        return JSON.parseObject(JSON.toJSONBytes(data), clazz);
    }
}
